#ifndef DEMO_GAMED3D
#define DEMO_GAMED3D

#include "EngineWin32.h"

struct Vertex
{
	static const D3DVERTEXELEMENT9 d3dve[4];
	static IDirect3DVertexDeclaration9* d3dvd;
	D3DXVECTOR3 v3Pos;
	D3DXVECTOR3 v3Norm;
	D3DXVECTOR2 v2Tex;
	Vertex(CONST D3DXVECTOR3& _v3Pos, CONST D3DXVECTOR3& _v3Norm, CONST D3DXVECTOR2& _v2Tex)
		:v3Pos(_v3Pos),v3Norm(_v3Norm),v2Tex(_v2Tex)
	{

	}
};

class MouseTrack
{
public:
	virtual void OnLeftButtonDown(WORD x, WORD y);
	virtual ~MouseTrack();
};

struct Geometry
{
	MouseTrack* mt;

	IDirect3DIndexBuffer9* d3dib;
	IDirect3DVertexBuffer9* d3dvb;

	IDirect3DTexture9* d3dtex;
	D3DXVECTOR4 v4Mate;

	D3DXMATRIX mWorld;
};

struct DirectLight
{
	D3DXVECTOR3 v3Color;
	D3DXVECTOR3 v3Dir;
};

struct PointLight
{
	D3DXVECTOR3 v3Color;
	D3DXVECTOR3 v3Pos;
	D3DXVECTOR3 v3Atte;
};

class EngineD3D : public EngineWin32
{
private:
	//Direct3D
	IDirect3D9* _d3d;
	
	//Error Buffer
	ID3DXBuffer* _dxbuf;
	
	//Effect
	ID3DXEffect* _dxfx;

	//vs output
	IDirect3DTexture9* _d3dtexRef;
	IDirect3DTexture9* _d3dtexMT;

	//retrieve
	IDirect3DSurface9* _d3dsurfRef;
	IDirect3DSurface9* _d3dsurfMT;
	bool _retrack;

	Geometry* _RetrieveGeometry(WORD x, WORD y);

protected:
	//Device
	IDirect3DDevice9* d3ddev;

	//Geometry
	list<Geometry*> geos;

	//Camera
	D3DXVECTOR3 v3ViewEye;
	D3DXVECTOR3 v3ViewTarget;
	D3DXVECTOR3 v3ViewUp;
	D3DXMATRIX mProj;

	//Light
	DirectLight sun;
	list<PointLight*> lights;

	//override redraw
	void Redraw();

	//override mouse
	void OnLeftButtonDown(WORD x, WORD y);

public:
	EngineD3D(LPCSTR title);
	virtual ~EngineD3D();
};

#endif//DEMO_GAMED3D
